import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'


Vue.use(VueRouter)

const routes = [
  {

    path: '/', redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "home" */ '../page/home.vue')
  },
  {
    path: '/signup',

    component: () => import(/* webpackChunkName: "signup" */ '../page/signup.vue')
  },
  {
    path: '/signin',

    component: () => import(/* webpackChunkName: "signin" */ '../page/signin.vue')
  },
  {
    path: '/answer',

    component: () => import(/* webpackChunkName: "answer" */ '../page/answer.vue')
  },
  {
    path: '/collect',

    component: () => import(/* webpackChunkName: "collect" */ '../page/collect.vue')
  },
  {
    path: '/centres',

    component: () => import(/* webpackChunkName: "collect" */ '../page/centres.vue')
  },

  {
    //没匹配到就回到主页
    path: '*', redirect: '/home'
  },
]



const router = new VueRouter({
  routes
})

//导航守卫
router.beforeEach((to, from, next) => {
  //to 访问路径
  //from 代表从个路径来
  //next 放行
  //注册和登录和主页页面可以放行
  if (to.path == '/signin') return next()
  if (to.path == '/signup') return next()
  if (to.path == '/home') return next()

  //获取token
  const tokenStr = store.getters.token
  console.log('beforeEach');
  if (!tokenStr) return next('/signin')
  next()
})

export default router
